<?php

//编译生成css文件
function parse_css($urls)
{
    $path = get_resource_path('css', $urls);
    return  "/" . $path . "?v=" . app_conf("DB_VERSION");
}

/**
 *
 * @param $urls 载入的脚本
 * @param $encode_url 需加密的脚本
 */
function parse_script($urls, $encode_url = array())
{
    $path = get_resource_path('js', $urls, $encode_url);
    return "/" . $path . "?v=" . app_conf("DB_VERSION");
}

function get_resource_path($type, $urls, $encode_url = null)
{
    $statics_path = 'public/runtime/statics/';
    $app_info = json_decode(@file_get_contents(APP_ROOT_PATH . $statics_path . 'app.json'), true);
    if (empty($app_info)) {
        $app_info = array();
    }

    $key = md5(implode(',', $urls));

    if (!IS_DEBUG && isset($app_info[$key]) && file_exists(APP_ROOT_PATH . $app_info[$key]['path'])) {
        return $app_info[$key]['path'];
    }

    if (!file_exists(APP_ROOT_PATH . $statics_path)) {
        mkdir(APP_ROOT_PATH . $statics_path, 0777, true);
    }

    switch ($type) {
        case 'js':
            $path = get_script_path($urls, $encode_url);
            break;
        case 'css':
            $path = get_css_path($urls);
            break;
        default:
            return;
    }

    if (empty($app_info[$key]) || $app_info[$key]['path'] != $path) {
        $app_info [$key] = array(
            'type' => $type,
            'path' => $path,
            'urls' => $urls,
            'encode_url' => $encode_url,
        );
        @file_put_contents(APP_ROOT_PATH . $statics_path . 'app.json', json_encode($app_info));
    }
    return $path;
}

function get_css_path($urls)
{
    $statics_path = 'public/runtime/statics/';
    $tmpl_path = $GLOBALS['tmpl']->_var['TMPL'];

    $css_content = '';
    foreach ($urls as $url) {
        $css_content .= @file_get_contents($url);
    }
    $css_content = preg_replace("/[\r\n]/", '', $css_content);
    $css_content = str_replace("../dist/statics/images/", $tmpl_path . "/dist/statics/images/", $css_content);
    $css_url = $statics_path . md5($css_content) . '.css';
    if (!file_exists(APP_ROOT_PATH . $css_url)) {
        @file_put_contents(APP_ROOT_PATH . $css_url, $css_content);
        if ($GLOBALS['distribution_cfg']['CSS_JS_OSS']) {
            syn_to_remote_file_server($css_url);
        }
    }
    return $css_url;
}

function get_script_path($urls, $encode_url = array())
{
    $statics_path = 'public/runtime/statics/';
    if (count($encode_url) > 0) {
        require_once APP_ROOT_PATH . "system/libs/javascriptpacker.php";
    }

    $js_content = '';
    foreach ($urls as $url) {
        $append_content = @file_get_contents($url) . "\r\n";
        if (in_array($url, $encode_url)) {
            $packer = new JavaScriptPacker($append_content);
            $append_content = $packer->pack();
        }
        $js_content .= $append_content;
    }

    $js_url = $statics_path . md5($js_content) . '.js';
    if (!file_exists(APP_ROOT_PATH . $js_url)) {
        @file_put_contents(APP_ROOT_PATH . $js_url, $js_content);
        if ($GLOBALS['distribution_cfg']['CSS_JS_OSS']) {
            syn_to_remote_file_server($js_url);
        }
    }
    return $js_url;
}

function get_promoter_type($user_id,$bm_promoter_type){
    $bm_promoter_type=intval($bm_promoter_type);
    $new_bm_promoter_type=0;
    if($user_id>0 && !$bm_promoter_type){
        $promoter_info=$GLOBALS['db']->getRow("select id,pid from ".DB_PREFIX."bm_promoter where user_id=".intval($user_id)." and status=1");
        if(!$promoter_info){
            $new_bm_promoter_type=0;
        }else{
            $new_bm_promoter_type=$promoter_info['pid'] >0?1:2;
        }

    }

    return $new_bm_promoter_type;
}

/*ajax返回*/
function api_ajax_return($data, $is_debug = false)
{

    if ($_REQUEST['post_type'] != 'json') {
        header("Content-Type:text/html; charset=utf-8");
        header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');

        //重新搜索bm_promoter_type
        if($GLOBALS['user_info']){
            $bm_promoter_type=intval($GLOBALS['user_info']['bm_promoter_type']);
            $GLOBALS['user_info']['bm_promoter_type']=$bm_promoter_type?$bm_promoter_type:get_promoter_type($GLOBALS['user_info']['id'],$bm_promoter_type);
            //$bm_promoter_type 0 登出，重新登录
            if(!$bm_promoter_type){
                fanwe_require(APP_ROOT_PATH."system/libs/user.php");
                $result = loginout_user();
                es_session::delete("user_info");

                //重新赋值
                $GLOBALS['user_info']=es_session::get("user_info");

                $data['user_login_status'] = 0;
                $data['status'] = 0;
                $data['error'] = '用户未登陆,请先登陆';
            }
        }

        $GLOBALS['tmpl']->assign('user_info',$GLOBALS['user_info']);

        //输出一、二、三级显示名称
        $bm_config = load_auto_cache("bm_config");
        $bm_one = $bm_config['bm_one'];
        $bm_two = $bm_config['bm_two'];
        $bm_three = $bm_config['bm_three'];
        $GLOBALS['tmpl']->assign('bm_one', $bm_one);
        $GLOBALS['tmpl']->assign('bm_two', $bm_two);
        $GLOBALS['tmpl']->assign('bm_three', $bm_three);
        
        //输出印票名称
        $m_config = load_auto_cache("m_config");
        $ticket_name=$m_config['ticket_name'];
        $GLOBALS['tmpl']->assign('ticket_name', $ticket_name);

        //模板输出
        $GLOBALS['tmpl']->assign('data', $data);
        if ($data['user_login_status'] === 0) {
            if (isset($_REQUEST['tmpl_pre_dir'])) {
                $tmpl_pre_dir = strim($_REQUEST['tmpl_pre_dir']);
                $tmpl_dir = "{$tmpl_pre_dir}/login-nav.html";
            } else {
                $tmpl_dir = "login-login.html";
            }
        } else {
            $tmpl_dir = $GLOBALS['class_name'] . '-' . $GLOBALS['act'] . '.html';
            if (isset($_REQUEST['tmpl_pre_dir'])) {
                $tmpl_pre_dir = strim($_REQUEST['tmpl_pre_dir']);
                $tmpl_dir = $tmpl_pre_dir . '/' . $tmpl_dir;
            }

        }
        $GLOBALS['tmpl']->display($tmpl_dir);
        die();
    } else {
        echo json_encode($data);
    }

}

?>